package com.cockatiel.statemachine.squirrel.foundation.config;

import com.cockatiel.statemachine.enums.ActivityEvent;
import com.cockatiel.statemachine.enums.ActivityState;
import com.cockatiel.statemachine.model.StatemachineContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.squirrelframework.foundation.fsm.StateMachineBuilderFactory;
import org.squirrelframework.foundation.fsm.UntypedStateMachine;
import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder;
import org.squirrelframework.foundation.fsm.annotation.StateMachineParameters;
import org.squirrelframework.foundation.fsm.impl.AbstractUntypedStateMachine;

/**
 * 流程引擎配置类
 *
 * @Author: louxiujun@kanzhun.com
 * @Date: 2023-06-10 22:14:38
 * @Description:
 */
@Configuration
public class SmsStatemachineConfigurer {

    // 构建状态转移
    @Bean
    public UntypedStateMachine untypedStateMachine() {
        UntypedStateMachineBuilder builder = StateMachineBuilderFactory.create(SmsStatemachine.class);
        builder.externalTransition().from(ActivityState.NOT_START).to(ActivityState.DATA_PREPARING).on(ActivityEvent.ACT_BEGIN).callMethod("notice");
        builder.externalTransition().from(ActivityState.DATA_PREPARING).to(ActivityState.DATA_PREPARED).on(ActivityEvent.FINISH_DATA_CAL).callMethod("notice");
        builder.externalTransition().from(ActivityState.DATA_PREPARED).to(ActivityState.DATA_PUSHING).on(ActivityEvent.FINISH_DATA_PREPARE).callMethod("notice");
        builder.externalTransition().from(ActivityState.DATA_PUSHING).to(ActivityState.FINISHED).on(ActivityEvent.FINISH_DATA_PUSHING).callMethod("notice");

        UntypedStateMachine fsm = builder.newStateMachine(ActivityState.NOT_START);
        return fsm;
    }

    @StateMachineParameters(stateType = ActivityState.class, eventType = ActivityEvent.class, contextType = StatemachineContext.class)
    public static class SmsStatemachine extends AbstractUntypedStateMachine {

        protected void notice(ActivityState from, ActivityState to, ActivityEvent event, StatemachineContext context) {
            System.out.println("【变更前状态】:" + from + ";【变更后状态】:" + to);
        }
    }
}
